/**
 *  增加或减少(输入) 选择的商品
 */
define(function(require,exports,module) {
    var myPub = require('./public');

    function  SetVideoNav(hrefSearch, name) {
        var oWindow = $(window);
        var resizeTimer = null;
        var oSildeNavScroll = $('.js_sildeNavScroll');  // 导航滚动条容器
        var oVideoNavJRoll = new myPub.ScrollBar();     // 导航滚动条
        var navHtml = '';

        var windowLocArg = window.location.search;

        // 获取数据添加导航滚动
        SetNavLi(hrefSearch, name, function () {
            oVideoNavJRoll.AddScroll(oSildeNavScroll[0], {
                scrollY: false,
                scrollX: true
            });

            ScrollAddClass();
        });

        // 页面尺寸修改
        oWindow.on('resize',function() {
            if (resizeTimer) {
                clearTimeout(resizeTimer)
            }
            resizeTimer = setTimeout(function(){
                if(oVideoNavJRoll.obj === undefined) {
                    oVideoNavJRoll.AddScroll(oSildeNavScroll[0], {
                        scrollY: false,
                        scrollX: true
                    });
                }
                else {
                    setUlWidth();
                    oVideoNavJRoll.ReScroll();
                }
            }, 400);

        });

        
        
        function ScrollAddClass() {
            if(windowLocArg !== undefined && windowLocArg !== '') {
                var _locString = windowLocArg.split('?')[1].split('=');
                // 设置判断条件 参数不是ID return 出去
                if(_locString[0] !== 'ID') return;

                var _windowLocArg = _locString[1].charAt(0);
                var scrollEle = $('.js_scroll_'+ _windowLocArg +'');
                var scrollLeft = scrollEle.position().left/2;
                oSildeNavScroll.find('li').removeClass('swiper-pagination-bullet-active');
                scrollEle.addClass('swiper-pagination-bullet-active');
                oVideoNavJRoll.ScrollTo(-scrollLeft,0,0);
            }
            else {
                oSildeNavScroll.find('li').eq(0).addClass('swiper-pagination-bullet-active');
            }
        }



        // 设置导航数据
        function SetNavLi(hrefSearch, name, fn) {
            for(var i=0; i<name.length; i++) {
                navHtml += '<li class="swiper-pagination-bullet js_scroll_'+ hrefSearch[i] +'" _ID="'+hrefSearch[i]+'"><a href="/video/toVideoList?ID='+ hrefSearch[i] +'">'+ name[i] +'</a></li>'
            }
            oSildeNavScroll.find('ul').append(navHtml);
            setUlWidth ();
            if(fn)fn();
        }

        function setUlWidth () {
            var navWidth = 0;
            oSildeNavScroll.find('li').each(function (i) {
                navWidth += Math.ceil(oSildeNavScroll.find('li').eq(i).outerWidth(true) + 0.5);
            });
            oSildeNavScroll.find('ul').css({'width': navWidth});
        }

    }

    exports.SetVideoNav = SetVideoNav;
});